package com.leetcode.algorithm.y22.m08.w4;

/**
 * 1004. 最大连续1的个数 III
 * 
 * https://leetcode.cn/problems/max-consecutive-ones-iii/
 * 
 * @author jie.deng
 *
 */
class Question1004Solution01 {
	
	public int longestOnes(int[] nums, int k) {
		int len = nums.length;
		int longest = 0;
		int invalidCnt = 0;
		int left = 0;
		for (int right = 0; right < len; right++) {
			if (nums[right] != 1) {
				invalidCnt++;
			}
			while (invalidCnt > k) {
				if (nums[left] != 1) {
					invalidCnt--;
				}
				left++;
			}
			longest = Math.max(longest, right - left + 1);
		}

		return longest;
	}
    
}